package com.sinya.shoppingmall.fragment;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sinya.shoppingmall.R;
import com.sinya.shoppingmall.bean.BaseResult;
import com.sinya.shoppingmall.https.VolleyParams;
import com.sinya.shoppingmall.https.VolleyRequest;
import com.sinya.shoppingmall.utils.DrawableUtils;
import com.sinya.shoppingmall.utils.UIUtils;
import com.sinya.shoppingmall.widget.FlowLayout;

import java.util.List;
import java.util.Random;

/**
 * 搜索记录、 热搜词条界面
 * @author KoizumiSinya
 * @date 2016/1/10.
 */
public class SearchHistoryFragment extends Fragment {

    private Context context;
    private View rootView;
    private LinearLayout ll_searchHistory;

    public static SearchHistoryFragment getInstance() {
        SearchHistoryFragment searchFragment = new SearchHistoryFragment();
        return searchFragment;
    }

    // [+] Handler

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (msg.obj != null) {
                        BaseResult baseResult = (BaseResult) msg.obj;
                        if (baseResult.searchKeywords != null && baseResult.searchKeywords.size() > 0) {
                            ll_searchHistory = (LinearLayout) rootView.findViewById(R.id.ll_search_container);
                            ll_searchHistory.addView(getHotSearchTexts(baseResult.searchKeywords));
                        }
                    }
                    break;
                default:
                    break;
            }

            return false;
        }
    });

    // [-] Handler

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity();
        rootView = inflater.inflate(R.layout.view_search_history_layout, null);
        return rootView;
    }

    public void getData() {
        VolleyRequest.Start(context, handler, 0, VolleyParams.searchRecommend);
    }

    private View getHotSearchTexts(List<String> searchTexts) {

        FlowLayout flowLayout = new FlowLayout(context);

        //
        flowLayout.setSpace(UIUtils.dip2px(context, 10), UIUtils.dip2px(context, 8));
        flowLayout.setPadding(UIUtils.dip2px(context, 10), UIUtils.dip2px(context, 10), UIUtils.dip2px(context, 10), UIUtils.dip2px(context, 10));

        for (int i = 0; i < searchTexts.size(); i++) {
            final String text = searchTexts.get(i);

            TextView textView = new TextView(context);
            textView.setText(text);
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(Color.WHITE);
            textView.setPadding(UIUtils.dip2px(context, 5), UIUtils.dip2px(context, 2), UIUtils.dip2px(context, 5), UIUtils.dip2px(context, 2));

            Random random = new Random();
            int red = random.nextInt(210) + 20;
            int green = random.nextInt(210) + 20;
            int bule = random.nextInt(210) + 20;

            int argb = Color.argb(0xff, red, green, bule);
            int color = Color.argb(0xff, 0xcc, 0xcc, 0xcc);

            GradientDrawable normalBg = DrawableUtils.getGradientDrawable(GradientDrawable.RECTANGLE, UIUtils.dip2px(context, 5), argb);
            GradientDrawable pressBg = DrawableUtils.getGradientDrawable(GradientDrawable.RECTANGLE, UIUtils.dip2px(context, 5), color);

            StateListDrawable selectorBg = DrawableUtils.getStateListDrawable(normalBg, pressBg);

            textView.setBackground(selectorBg);
            flowLayout.addView(textView);

            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
                }
            });
        }
        return flowLayout;
    }
}
